package com.all.ui;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import com.all.base.BaseActivity;
import com.all.ui.adapter.DynaTreeAdapter;
import com.all.utils.FileUtils;
import com.all.widget.tree.Node;
import com.all.widget.tree.OnTreeNodeClickListener;

public class TreeListActivityDyna extends BaseActivity {

	Context mContext;
	List<Node> mNodeList = new ArrayList<Node>();
	private ListView mTree;
	private ListView mList;
	
	//下载数据Handler
	private Handler mLoadColumnHandler ;
	//下载进度条
	ProgressDialog mProgressDialog;
	//下载回返的字条串结果
	protected String mResultString;
	//判断是否是测试
	boolean isDebug = false;
	
	private DynaTreeAdapter mAdapter;
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_tree_activity);
		mContext = this;
		intiParams();
		loadTreeData();
		initTreeList();
		
	}
	private void intiParams() {
		// TODO Auto-generated method stub
		mProgressDialog = new ProgressDialog(mContext);
		mTree = (ListView) findViewById(R.id.id_tree);
		isDebug=true;
		
		mLoadColumnHandler = new Handler(){
		

			/********  MODIFY*********/
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				mProgressDialog.dismiss();
				super.handleMessage(msg);
				//BasicRusult result = (BasicRusult)msg.obj;
				if(isDebug)
				{
					switch(msg.arg1)
					{
						case 1:
							try
							{
								mAdapter = new DynaTreeAdapter(mTree,mContext,mResultString);
								mAdapter.setOnTreeNodeClickListener(new OnTreeNodeClickListener()
								{
									@Override
									public void onClick(Node node, int position)
									{
										if (!node.isLeaf()&&node.getChildren().size()==0)
										{
											loadChildNodeData();
										}else if(node.isLeaf())
										{
											loadPersonData();
										}
									}
	
								});
	
							} catch (Exception e)
							{
								e.printStackTrace();
							}
							mTree.setAdapter(mAdapter);
						break;
						case 2:
							mAdapter.addChild(mResultString);
							
						break;
						case 3:
							//addListData(mResultString);
						break;
					}
				}/*else(result.isSuccuss())
				{
					
						
					}
					//mDatas = parseResult();
					//mNodeList = json2Node(mNodeList,mResultString,2);
					
				}*/
			}
			
		};
		
	}
	private void loadTreeData() {
		// TODO Auto-generated method stub
		mProgressDialog.setMessage("正在下载字段信息");
		mProgressDialog.show();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
			
				if(isDebug)
				{
					/********使用测试数据  MODIFY*********/
					try {
						mResultString=getCorrectPts("select.txt");
						Message msg = new Message();
						msg.arg1=1;
						mLoadColumnHandler.sendMessage(msg);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else
				{
					// 表字段返回结果
					/*BasicRusult basicRusult = null;
					//获取表字段
		
						String act = "selectActorsTree";
						BasicNameValuePair pair1 = new BasicNameValuePair("actorType", actorType);
						BasicNameValuePair pair2 = new BasicNameValuePair("actorsId", actorsId);
						BasicNameValuePair pair3 = new BasicNameValuePair("id", id);
						BasicNameValuePair pair4 = new BasicNameValuePair("type", type);
						//下载选择的节点树数据
						basicRusult = WorkflowBLL.getDynaInfo(mContext, act,pair1,pair2,pair3,pair4);
					if (basicRusult.isSuccuss()&&!StringUtils.isEmpty(basicRusult.getData())) {
						//获取返回的内容
						mResultString = basicRusult.getData();
						Message msg = new Message();
						msg.obj = basicRusult;
						mLoadColumnHandler.sendMessage(msg);
					}
					else
					{
						Toast.makeText(mContext, "下载字段失败", Toast.LENGTH_SHORT).show();
					}*/
				}
			}
		}).start();
	}
	
	
	private void loadChildNodeData() {
		// TODO Auto-generated method stub
		
		mProgressDialog.setMessage("正在下载字段信息");
		mProgressDialog.show();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
			
				/********使用测试数据  MODIFY*********/
				if(isDebug)
				{
					try {
						mResultString=getCorrectPts("select_two.txt");
						Message msg = new Message();
						msg.arg1=2;
						mLoadColumnHandler.sendMessage(msg);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else
				{
					// 表字段返回结果
					/*BasicRusult basicRusult = null;
					//获取表字段
		
						String act = "selectActorsTree";
						BasicNameValuePair pair1 = new BasicNameValuePair("actorType", actorType);
						BasicNameValuePair pair2 = new BasicNameValuePair("actorsId", actorsId);
						BasicNameValuePair pair3 = new BasicNameValuePair("id", id);
						BasicNameValuePair pair4 = new BasicNameValuePair("type", type);
						//下载选择的节点树数据
						basicRusult = WorkflowBLL.getDynaInfo(mContext, act,pair1,pair2,pair3,pair4);
					if (basicRusult.isSuccuss()&&!StringUtils.isEmpty(basicRusult.getData())) {
						//获取返回的内容
						mResultString = basicRusult.getData();
						Message msg = new Message();
						msg.obj = basicRusult;
						mLoadColumnHandler.sendMessage(msg);
					}
					else
					{
						Toast.makeText(mContext, "下载字段失败", Toast.LENGTH_SHORT).show();
					}*/
				}
				
			}
		}).start();
	}
	
	
	private void loadPersonData() {
		// TODO Auto-generated method stub
		
		mProgressDialog.setMessage("正在下载字段信息");
		mProgressDialog.show();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
			
				if(isDebug)
				{
					/********使用测试数据  MODIFY*********/
					try {
						mResultString=getCorrectPts("select_th.txt");
						Message msg = new Message();
						msg.arg1= 3;
						mLoadColumnHandler.sendMessage(msg);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				else
				{
					// 表字段返回结果
					/*BasicRusult basicRusult = null;
					//获取表字段
		
						String act = "selectActorsTree";
						BasicNameValuePair pair1 = new BasicNameValuePair("actorType", actorType);
						BasicNameValuePair pair2 = new BasicNameValuePair("actorsId", actorsId);
						BasicNameValuePair pair3 = new BasicNameValuePair("id", id);
						BasicNameValuePair pair4 = new BasicNameValuePair("type", type);
						//下载选择的节点树数据
						basicRusult = WorkflowBLL.getDynaInfo(mContext, act,pair1,pair2,pair3,pair4);
					if (basicRusult.isSuccuss()&&!StringUtils.isEmpty(basicRusult.getData())) {
						//获取返回的内容
						mResultString = basicRusult.getData();
						Message msg = new Message();
						msg.obj = basicRusult;
						mLoadColumnHandler.sendMessage(msg);
					}
					else
					{
						Toast.makeText(mContext, "下载字段失败", Toast.LENGTH_SHORT).show();
					}*/
				}
			}
		}).start();
	}
	private void initTreeList() {
		// TODO Auto-generated method stub
		
	}
	
	private String getCorrectPts(String path) throws IOException {
		String str;
		InputStream input = getResources().getAssets().open(path);
		
		str = FileUtils.readInputStream(input);
		return str.trim();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
